package com.google.code.bankagregator.adapter.bank.imex.parser.credit;

import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.adapter.exception.AdapterException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 13.06.13
 * Time: 3:30
 * To change this template use File | Settings | File Templates.
 */
public class ImexAutoCreditParser {
    private static final int INDEX_TD_FOR_CREDIT = 1;
    private static final int INDEX_TR_WITH_RATE = 1;
    private static final int INDEX_TR_WITH_MAXAMOUNT = 2;
    private static final int INDEX_TR_WITH_TERM = 3;
    private static final int INDEX_TR_WITH_PREPAYMENT = 4;
    private static final String SELECT_PARAM = "div.show-box table tr";
    private static final String CREDIT_URL = "http://imexbank.com.ua/rus/crossselling/";
    private Document con = null;
    private Bank bank = null;
    private Credit credit;

    public ImexAutoCreditParser() throws AdapterConnectException {
        bank = new Bank();
        bank.setName("АО \"ИМЭКСБАНК\"");
        bank.setUrl("http://imexbank.com.ua/rus/main.phtml");
        try {
            con = Jsoup.connect(CREDIT_URL).timeout(20000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
        credit = new Credit();
    }

    /**
     * Get credit`s params
     *
     * @return credit`s params
     * @throws AdapterException
     */
    public List<Credit> getCredit() {
        List<Credit> creditParam = new ArrayList<>();
        Element rateEl = con.select(SELECT_PARAM).get(INDEX_TR_WITH_RATE).
                select("td").get(INDEX_TD_FOR_CREDIT);
        Element maxAmountEl = con.select(SELECT_PARAM).get(INDEX_TR_WITH_MAXAMOUNT).
                select("td").get(INDEX_TD_FOR_CREDIT);
        Element termEl = con.select(SELECT_PARAM).get(INDEX_TR_WITH_TERM).
                select("td").get(INDEX_TD_FOR_CREDIT);
        Element prepEl = con.select(SELECT_PARAM).get(INDEX_TR_WITH_PREPAYMENT).
                select("td").get(INDEX_TD_FOR_CREDIT);

        double rate = Double.parseDouble(rateEl.text().replace(",", ".").replace("%", ""));
        int maxAmount = Integer.parseInt(maxAmountEl.text().replaceAll("[^0-9.]+", "").replace(".", "")) * 1000;
        int term = Integer.parseInt(termEl.text().replaceAll("[^0-9.]+", "")) * 31;
        double prepayment = Double.parseDouble(prepEl.text().replaceAll("[^0-9.]+", ""));

        credit.setCreditRate(rate);
        credit.setMaxAmount(maxAmount);
        credit.setTerm(term);
        credit.setPrepayment(prepayment);
        credit.setName("Кредит на авто");
        credit.setCurrency(Currency.UAH);
        credit.setType(CreditType.AUTO);
        credit.setBank(bank);
        credit.setUrl(CREDIT_URL);
        credit.setState(State.ACTIVE);
        creditParam.add(credit);
        return creditParam;
    }
}
